package com.oceansoft.mobile.biz.admin.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.oceansoft.mobile.biz.front.entity.AppPlugins;
import com.oceansoft.mobile.biz.front.entity.AppPlugins_api;
import com.oceansoft.mobile.common.base.entity.Pager;
import com.oceansoft.mobile.common.base.entity.Query;
import com.oceansoft.mobile.common.base.entity.Result;
import com.oceansoft.mobile.common.dao.ICommonDao;
import com.oceansoft.mobile.common.util.CacheBaseService;

@Service
public class ApplyadminServiceImpl implements IApplyadminService{
	 @Resource
	 private ICommonDao commonDao;
	
	@Override
	public Pager findApplyInfo(Query query, String keywords) {
		 String page_sql = "select T.* from t_app_plugins T where 1=1  ";
	        if (keywords != null && !keywords.equals("")) {
	            page_sql += " and T.title like '%"+keywords+"%'";
	        }
	        String count=page_sql;
	        page_sql += Query.getPageSQL("T", query);
	        String count_sql="select count(1) from ("+count+") b";
	       // count_sql += Query.getCountSQL("T", query);
	        long total = commonDao.getCount(count_sql);
	        List list = commonDao.loadMapList(page_sql);
	        return new Pager(total, query.getPage(), list);
	}

	@Override
	public Result deleteApplyById(int id) {
		int rt = commonDao.deleteOne("DELETE FROM t_app_plugins WHERE ID = ?", id);
		if (rt > 0)
			return new Result();
		return new Result(Result.FAILURE, "删除不成功");
	}

	@Override
	public AppPlugins loadApplyById(int id) {
		String sql = "select * from t_app_plugins WHERE ID = ?";
		AppPlugins art = commonDao.loadOne(sql,
				AppPlugins.getFullNewsMapper(), id);
		return art;
	}

	@Override
	public Result saveOrUpdate(AppPlugins apply) {
		if (null != apply) {
			// 更新
			commonDao
					.updateOne(
							"UPDATE t_app_plugins t SET t.title = ? ,t.icon = ? ,t.url = ?,t.description = ?  ,t.startpage = ?  ,t.page= ?  ,t.cate= ? ,t.state= ?  ,t.ish5= ?  WHERE t.id =? ",
							apply.getTitle(), apply.getIcon(),apply.getUrl(),
							apply.getTitle(),apply.getStartPage(),apply.getStartPage(),
							apply.getCate(),apply.getState(),apply.getIsh5(),
							apply.getId()
							);
			/**
			 * 更新缓存
			 */
			getRefresh(apply);
			return new Result();
		} else {
			return new Result(Result.FAILURE, "信息保存失败");
		}
	}

	@Override
	public Result saveApply(AppPlugins apply) {
		try {
			// 保存产品主表信息
			commonDao
					.saveOne(
							"INSERT INTO t_app_plugins(title,icon,url,description,apptype,startpage,page,pagesize,cate,ordernum,state,ish5) VALUES"
									+ "(:title,:icon,:url,:description,:appType,:startPage,:page,:pagesize,:cate,:orderNum,:state,:ish5)",
									apply);
			commonDao
			.updateOne(
					"UPDATE t_app_plugins t SET t.ordernum = t.id where t.ordernum='0'",
					""
					);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		/**
		 * 更新缓存
		 */
		getRefresh(apply);
		// return new Result(Result.FAILURE, "保存失败");
		return new Result();
	}
		private void getRefresh(AppPlugins_api apply)
		{
			String whereSQL = "";
			whereSQL = " and cate='" + apply.getCate() + "'";
			Result r = null;
			String key = "AppPlugins_api_" +apply.getCate();
			List<AppPlugins_api> list = commonDao.loadList(
						"select * from t_app_plugins where state=1 " + whereSQL
								+ " order by ordernum",
						ParameterizedBeanPropertyRowMapper
								.newInstance(AppPlugins_api.class));
			r = new Result();
			r.setData(list);// cate: 1(微服务) 2(微警情) 3(微应用) orderNum:排序字段
			CacheBaseService.getInstance().cache(key, r, 1f);
			
			CacheBaseService.getInstance().delCache("AppPlugins_api_0");
			CacheBaseService.getInstance().delCache("AppPlugins_api");
			
			
			
				
				
			String key2 = "AppPlugins";
        	List<AppPlugins> list2 = commonDao.loadList("select * from t_app_plugins ", ParameterizedBeanPropertyRowMapper.newInstance(AppPlugins.class));
        	Result r2 = new Result();
            r2.setData(list2);
            CacheBaseService.getInstance().cache(key2, r2, 1f);
			
		}
	}
